/*
 * @Description: 添加档案鉴定管理弹窗组件
 * @Author: Rfan
 * @Date: 2022-07-29 16:18:02
 * @LastEditTime: 2022-08-09 19:44:09
 */

import { useEffect, useState } from 'react';
import { Button, Form, Input, Modal, Select } from 'antd';
import { fetchFondsListByUnitId } from '@/api/fonds';
import { fetchATListByFondId } from '@/api/archivesType';
import { appState } from '@/store/mobx';
import { useAppraisalAuditContext } from '../../contexts/AppraisalAuditContext';
import type { FC } from 'react';
import type { ICommonDialog } from '@/interfaces/global';
import type { IArchiveType } from '@/interfaces/archiveType';
import type { IFond } from '@/interfaces/fond';
import type { IDispositionPendingBatchParams } from '@/api/archives/disposition';
import { IAddDispositionParams } from '@/api/archives/disposition';
import AuditFormItem from '@/view/appraisalAndDisposition/AppraisalAudit/components/dialogs/common/AuditFormItem';
import { formatDate } from '@/utils';

const { Option } = Select;

// 鉴定类型
// const appraisalType = [
//   { id: 1, name: '价值鉴定' },
//   { id: 2, name: '开放鉴定' },
//   { id: 3, name: '销毁鉴定' }
// ];

interface AddDialogProps extends ICommonDialog {
  fondList?: IFond[];
  ATList?: IArchiveType[];
  currentFondId?: number;
  currentATId?: number;
}

const AddDialog: FC<AddDialogProps> = (props) => {
  const { visible, onClose } = props;
  const { addDispositionAuditFun } = useAppraisalAuditContext();
  const [form] = Form.useForm();

  useEffect(() => {
    if (visible) {
      form.setFieldsValue({ applyTime: formatDate() });
    }
  }, [visible]);

  const submitFunc = () => {
    form.submit();
  };

  type IAddForm = Pick<
    IAddDispositionParams,
    'archiveTypeId' | 'description' | 'title' | 'applyTime'
  >;
  const onFinish = (values: IAddForm) => {
    const params: IAddDispositionParams = { ...values, applyFormType: 1 };
    addDispositionAuditFun(params).then(() => {
      onCancel();
    });
  };

  const onCancel = () => {
    onClose();
  };

  return (
    <Modal
      destroyOnClose
      title="新建审核单"
      visible={visible}
      width={500}
      onCancel={onCancel}
      footer={[
        <Button key="cancel" onClick={onCancel}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={submitFunc}>
          确定
        </Button>
      ]}
    >
      <Form
        form={form}
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 14 }}
        preserve={false}
        onFinish={onFinish}
      >
        <AuditFormItem visible={visible} />
      </Form>
    </Modal>
  );
};

export default AddDialog;
